/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package 最终版学生成绩管理系统;

import java.awt.event.*;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.swing.JButton;
import javax.swing.JOptionPane;

public class DeleteStudent extends StudentEdit implements ActionListener {
	JButton b3=new JButton("删除");
	
	public DeleteStudent(){
		super();
		
		p12.add(b3);
		//对非必要文本框设置不可编辑
		s.setEnabled(false);
		name.setEditable(false);
		birth.setEditable(false);
		grade.setEnabled(false);
		clas.setEnabled(false);
		chinese.setEditable(false);
		english.setEditable(false);
		maths.setEditable(false);
		major.setEnabled(false);
		b1.addActionListener(this);
		b2.addActionListener(this);
		b3.addActionListener(this);
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO 自动生成的方法存根
		if(e.getActionCommand()=="确定"){
			String number=num.getText();
			if(number.equals("")){
				JOptionPane.showMessageDialog(null, "不存在此学生", "Messages",
						JOptionPane.INFORMATION_MESSAGE);	
			}
			else
			try{
				Connection con=DataBaseCreate.getConnection();
				Statement sql=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
				//查找Num字段中与number相等的数据
				ResultSet rs=sql.executeQuery("select * from student where Num like '%"+number+"%'");
				
				if(rs.next()){
				//显示到文本框	
				s.setSelectedItem(rs.getString("Sex"));
				name.setText(rs.getString("Name"));
				birth.setText(rs.getString("Birth"));
				chinese.setText(rs.getString("Chinese"));
				english.setText(rs.getString("English"));
				maths.setText(rs.getString("Maths"));
				s.setSelectedItem(rs.getString("Sex"));
				clas.setSelectedItem(rs.getString("Class"));
				grade.setSelectedItem(rs.getString("Grade"));
				major.setSelectedItem(rs.getString("Major"));
				}
				else{
					JOptionPane.showMessageDialog(null, "不存在此学生", "Messages",
							JOptionPane.INFORMATION_MESSAGE);
				}
				
				sql.close();
				con.close();
				
			}catch(java.lang.ClassNotFoundException err){
				JOptionPane.showMessageDialog(null, err.getMessage(), "ClassNotFoundException:",
					JOptionPane.INFORMATION_MESSAGE);
			}catch (SQLException ex){
				JOptionPane.showMessageDialog(null, ex.getMessage(), "SQLException: ", JOptionPane.INFORMATION_MESSAGE);
			}
		}
		else if(e.getActionCommand()=="取消"){
			setVisible(false);
		}
		else if(e.getActionCommand()=="删除"){
			String number=num.getText();
			if(number.equals("")){
				JOptionPane.showMessageDialog(null, "不存在此学生", "Messages",
						JOptionPane.INFORMATION_MESSAGE);	
			}
			else
			try{
				Connection con=DataBaseCreate.getConnection();
				Statement sql=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
				ResultSet rs=sql.executeQuery("select * from student where Num like '%"+number+"%'");
				
				if(rs.next()){
					//删除number所在行数据
					rs.deleteRow();
					//清空文本框
					s.setSelectedItem("");
					num.setText("");
					name.setText("");
					birth.setText("");
					chinese.setText("");
					english.setText("");
					maths.setText("");
					s.setSelectedItem("");
					clas.setSelectedItem("");
					grade.setSelectedItem("");
					major.setSelectedItem("");
					
					JOptionPane.showMessageDialog(null, "删除成功", "Messages",
							JOptionPane.INFORMATION_MESSAGE);
				}
				else{
					JOptionPane.showMessageDialog(null, "不存在此学生", "Messages",
							JOptionPane.INFORMATION_MESSAGE);
				}
				
				sql.close();
				con.close();
				setVisible(false);
				
			}catch(java.lang.ClassNotFoundException err){
				JOptionPane.showMessageDialog(null, err.getMessage(), "ClassNotFoundException:",
						JOptionPane.INFORMATION_MESSAGE);
				}catch (SQLException ex){
					JOptionPane.showMessageDialog(null, ex.getMessage(), "SQLException: ", JOptionPane.INFORMATION_MESSAGE);
				}
			
		}
	}
}

